package com.egreen.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.asset.dao.PointAccountDao;
import com.egreen.asset.dao.OperatePointRecordingDao;
import com.egreen.asset.service.IOperatePointRecordingService;
import com.egreen.common.pojo.DTO.asset.OperatePointRecordingDTO;
import com.egreen.common.pojo.entity.asset.OperatePointRecording;
import com.egreen.common.pojo.entity.asset.PointAccount;
import com.egreen.common.pojo.VO.asset.OperatePointRecordingVO;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Log
@Service
public class OperatePointRecordingServiceImpl extends ServiceImpl<OperatePointRecordingDao, OperatePointRecording> implements IOperatePointRecordingService {
    @Autowired
    OperatePointRecordingDao operatePointRecordingDao;

    @Autowired
    PointAccountDao pointAccountDao;

    /**
     * 积分的增加与减少
     *
     * @param operatePointRecordingDTO
     */

    @Override
    public void updateByPoint(OperatePointRecordingDTO operatePointRecordingDTO) {//+600
        //通过pointAccountId获取账户的积分余额
        PointAccount pointAccount = pointAccountDao.selectById(operatePointRecordingDTO.getPointAccountId());
        Integer pointBalance = pointAccount.getPointBalance();//500

        //对用户操作进行判断
        if (operatePointRecordingDTO.getOperate().equals("增加")) {
            pointBalance = pointBalance + operatePointRecordingDTO.getOperatePointNum();
        } else {
            //前者与后者比较，若前者大则返回1
            //说明减少的积分大于用户积分余额，所以会取不出 需要抛出异常
            if ((operatePointRecordingDTO.getOperatePointNum()).compareTo(pointAccount.getPointBalance()) == 1) {
                return;
            } else {
                pointBalance = pointBalance - operatePointRecordingDTO.getOperatePointNum();
            }
        }
        //将修改后的积分余额传回去
        pointAccount.setPointBalance(pointBalance);

        //新建操作记录并修改各字段
        OperatePointRecording operatePointRecording = new OperatePointRecording();
        operatePointRecording.setOperate(operatePointRecordingDTO.getOperate());
        operatePointRecording.setPointAccountId(operatePointRecordingDTO.getPointAccountId());
        operatePointRecording.setOperatedBalance(pointBalance);
        operatePointRecording.setOperatePointNum(operatePointRecordingDTO.getOperatePointNum());
        operatePointRecording.setTime(LocalDateTime.now());

        //对操作记录进行保存
        operatePointRecordingDao.insert(operatePointRecording);

        //对操作记录进行返回更新
        pointAccountDao.updateById(pointAccount);

    }

    /**
     * 通过用户ID查询积分操作记录
     *
     * @param
     */

    @Override
    public List<OperatePointRecordingVO> selectPointById(Long pointAccountId) {
        //存放查询的条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "point_account_id", "operate", "time", "operate_point_num", "operated_balance", "deleted");
        //进行筛选
        queryWrapper.eq("point_account_id", pointAccountId);
        //根据条件查询得到数据封装到集合里
        List<OperatePointRecordingVO> list = operatePointRecordingDao.selectList(queryWrapper);
        return list;
    }
}
